//
//  AudioPlayerViewController.m
//  Project_A
//
//  Created by running on my way on 15/1/17.
//  Copyright (c) 2015年 Flowers. All rights reserved.
//

#import "AudioPlayerViewController.h"
#import "UIImageView+WebCache.h"
#import <AVFoundation/AVFoundation.h>
#import "AFNetworking.h"
#import <MediaPlayer/MediaPlayer.h>
#import "AppDelegate.h"
@interface AudioPlayerViewController ()
//创建播放器
@property (nonatomic, retain)AVPlayer *player;
//添加图片
@property (nonatomic, retain)UIImageView *image;
//控制播放的按钮;
@property (nonatomic, retain)UIButton *playBut;
//下一曲按钮
@property (nonatomic, retain)UIButton *nextBut;
//上一曲按钮
@property (nonatomic, retain)UIButton *preBut;
//播放转动图
@property (nonatomic, retain)UIImageView *largeImage;
//播放进度条
@property (nonatomic, retain)UIProgressView *progress;

@end

@implementation AudioPlayerViewController{
    
    UIAlertView *alertView;
}
- (void)dealloc
{
    [_audioId release];
    [_PicUrl release];
    [_player release];
    [_image release];
    [_largeImage release];
    [_largePic release];
    [_progress release];
    [_item release];
    [super dealloc];
}
//在view将要出现时隐藏tableBar
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   self.view.backgroundColor = [UIColor whiteColor];
    //通过数组将所有的音频地址传过来, 再根据上一页cell的indexpath 取出当前cell里的音频地址
    NSURL *url = [NSURL URLWithString:[self.urlArray objectAtIndex:self.index]];
//    NSData *data = [NSData dataWithContentsOfURL:url];
//    
//    [[ShareHandle Player] initWithData:data error:nil];
//     [ShareHandle Player].volume = 0.8;
//    [[ShareHandle Player] play];
    //AVplayer 的资源管理
    self.item = [[AVPlayerItem alloc] initWithURL:url];
    
    //播放停止
    [[ShareHandle avPlayer] pause];
    //创建单例播放器 传进url
    [[ShareHandle avPlayer] initWithPlayerItem:self.item];
    //播放器默认声音
    [ShareHandle avPlayer].volume = 0.8;
    //播放
    [[ShareHandle avPlayer] play];
    //视图控制器上添加控件
    [self creatSubView];
    
}
//添加控件
- (void)creatSubView
{
    //imageVIew 显示播放音频对应的图片
    self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 200)];
    NSString *str = self.PicUrl;
    NSURL *url = [NSURL URLWithString:str];
    //用sdWebimage 显示图片 添加了占位图
    [self.image sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"open1.jpg"]];
    [self.view addSubview:self.image];
//    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(10, self.image.frame.size.height + 55, 355, 5)];
//    self.slider.value = 0;
//    [self.view addSubview:self.slider];
    self.progress = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.progress.frame = CGRectMake(10, self.image.frame.size.height + 50, 355, 5);
    self.time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(progressPlay) userInfo:nil repeats:YES];
    [self.view addSubview:self.progress];
    [self.progress release];
    self.largeImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.image.frame.size.height + 60, 130, 130)];
    NSURL *largeUrl = [NSURL URLWithString:self.largePic];
    [self.largeImage sd_setImageWithURL:largeUrl placeholderImage:[UIImage imageNamed:@"open1.jpg"]];
    [self.view addSubview:self.largeImage];
    self.largeImage.layer.masksToBounds = YES;
    self.largeImage.layer.cornerRadius = 65;
    [self.largeImage release];
   [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(change) userInfo:nil repeats:YES];
    
    //播放按钮
    self.playBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playBut.frame = CGRectMake(self.largeImage.frame.origin.x + self.largeImage.frame.size.width + 90, self.largeImage.frame.origin.y + 70, 50, 50);
    [self.playBut setBackgroundImage:[UIImage imageNamed:@"Pauses@2x.png"] forState:UIControlStateNormal];
    [self.playBut addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playBut];
    self.isSelected = NO;
    //下一首按钮
    self.nextBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextBut.frame = CGRectMake(self.playBut.frame.origin.x + 70, self.playBut.frame.origin.y, 50, 50);
    [self.nextBut setBackgroundImage:[UIImage imageNamed:@"NextA.png"] forState:UIControlStateNormal];
    [self.nextBut addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextBut];
    //上一首按钮
    self.preBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.preBut.frame = CGRectMake(self.playBut.frame.origin.x - 70, self.nextBut.frame.origin.y, 50, 50);
    [self.preBut setBackgroundImage:[UIImage imageNamed:@"OnA.png"] forState:UIControlStateNormal];
    [self.preBut addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.preBut];
    
}
//播放按钮的触发方法
- (void)playButton:(UIButton *)button
{    //判断状态 切换播放按钮的图片 控制播放状态
    if (self.isSelected == YES) {
        [self.playBut setBackgroundImage:[UIImage imageNamed:@"Pauses@2x.png"] forState:UIControlStateNormal];
        [[ShareHandle avPlayer] play];
        self.isSelected = NO;
    }else if (self.isSelected == NO){
        [self.playBut setBackgroundImage:[UIImage imageNamed:@"Play@2x.png"] forState:UIControlStateNormal];
          [[ShareHandle avPlayer] pause];
        self.isSelected = YES;
    }
}
//上一曲 / 下一曲的按钮触发方法
- (void)actionButton:(UIButton *)button
{
    //判断是哪个按钮
    if (button == self.preBut) {
        //判断是不是第一首歌 如果是就用alertView提示, 如果不是 变为上一曲 再播放
        if (self.index == 0) {
            alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"该曲为第一曲" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"知道了", nil];
            [alertView show];
            [alertView release];
            
        }else
        {
            //让音频数组中的元素下标减一
            self.index--;
            //找到减一后的音频地址
            NSURL *url = [NSURL URLWithString:[self.urlArray objectAtIndex:self.index]];
            //停止正在播放的单例播放器
            [[ShareHandle avPlayer] pause];
            //创建单例播放器 传入url
            [[ShareHandle avPlayer] initWithURL:url];
            //默认音量 0.1~1
            [ShareHandle avPlayer].volume = 0.8;
            //播放
            [[ShareHandle avPlayer] play];
        }
        //判断按钮是下一曲
    }else if (button == self.nextBut){
        //判断是不是最后一曲,是就提示是最后一曲, 若不是, 进入下一曲
        if (self.index == (self.urlArray.count - 1)) {
            alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"该曲为最后一曲" delegate:self cancelButtonTitle:@"取消了" otherButtonTitles:@"知道了", nil];
            [alertView show];
            [alertView release];
        }else
        {
            //让音频数组中的元素下标加一 
            self.index++;
            //穿件加一后的新音频地址
            NSURL *url = [NSURL URLWithString:[self.urlArray objectAtIndex:self.index]];
            //让原来的单例播放器停止播放
                [[ShareHandle avPlayer] pause];
            //初始化单例播放器 并且传入url
            [[ShareHandle avPlayer] initWithURL:url];
            [ShareHandle avPlayer].volume = 0.8;
            //播放
            [[ShareHandle avPlayer] play];
     }
 }
    
}
//旋转图
- (void)change
{
    if (self.isSelected == NO) {
      
    self.largeImage.transform = CGAffineTransformRotate(self.largeImage.transform, (M_PI_4));
    }
}
//进度播放条
- (void)progressPlay
{
    float value = CMTimeGetSeconds([ShareHandle avPlayer].currentItem.currentTime) / CMTimeGetSeconds([ShareHandle avPlayer].currentItem.duration);//播放器的当前播放资源的播放进度 和 当前播放资源的总时常;
    // 设置值
    self.progress.progress = value;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
